﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using BitAuto.Utils.Data;
using BitAuto.EP.CRMLogSystem.Utils;
using System.Data;
using BitAuto.EP.CRMLogSystem.Entity;

namespace BitAuto.EP.CRMLogSystem.DAL
{
    public class SQLDBConfigurationDAL
    {
        /// <summary>
        ///
        /// </summary>
        public IList<LogTableInfo> GetLogTableInfo()
        {
            IList<LogTableInfo> list = new List<LogTableInfo>();
            string sqlStr = @"select l.LogTableId,d.ConnectionString,l.TableName
from dbo.LogTable l with(nolock)
inner join dbo.DBConfig d with(nolock) on l.DBConfigId=d.DBConfigId
where l.IsActive=1";
            using (SqlDataReader dr = SqlHelper.ExecuteReader(AppConfig.BusinessOpportunityLog, CommandType.Text, sqlStr))
            {
                while (dr.Read())
                {
                    list.Add(Populate_LogTableInfo_FromDr(dr));
                }
            }
            return list;
        }
        /// <summary>
        ///
        /// </summary>
        public IList<LogFieldInfo> GetLogFieldInfo()
        {
            IList<LogFieldInfo> list = new List<LogFieldInfo>();
            string sqlStr = @"select l.LogTableId,l.FieldName,l.FieldType
from dbo.LogField l with(nolock)
where l.IsActive=1";
            using (SqlDataReader dr = SqlHelper.ExecuteReader(AppConfig.BusinessOpportunityLog, CommandType.Text, sqlStr))
            {
                while (dr.Read())
                {
                    list.Add(Populate_LogFieldInfo_FromDr(dr));
                }
            }
            return list;
        }
        /// <summary>
        /// 得到  allocationlog 数据实体
        /// </summary>
        /// <param name="dr">dr</param>
        /// <returns>allocationlog 数据实体</returns>
        public LogTableInfo Populate_LogTableInfo_FromDr(IDataReader dr)
        {
            LogTableInfo Obj = new LogTableInfo();
            Obj.LogTableId = dr["LogTableId"].ToString();
            Obj.ConnectionString = dr["ConnectionString"].ToString();
            Obj.TableName = dr["TableName"].ToString();
            return Obj;
        }
        /// <summary>
        /// 得到  allocationlog 数据实体
        /// </summary>
        /// <param name="dr">dr</param>
        /// <returns>allocationlog 数据实体</returns>
        public LogFieldInfo Populate_LogFieldInfo_FromDr(IDataReader dr)
        {
            LogFieldInfo Obj = new LogFieldInfo(dr["FieldName"].ToString(), dr["FieldType"].ToString()) { LogTableId = dr["LogTableId"].ToString() };
            return Obj;
        }
    }
}
